//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Eas.WebCrawler.PlugIns.FileNamers {
    /// <summary>A static helper class with functions that can be usefull when implementing the <see cref="FileNamer"/> interface.</summary>
    public static class NameHelper {
        private static char[] invalidChars = System.IO.Path.GetInvalidFileNameChars();
        static NameHelper() {
            Array.Sort<char>(invalidChars);
        }

        /// <summary>Takes a passed in <paramref name="originalFileName"/> and checks if a file with that name already exists in the passed-in <paramref name="directory"/> and if so returns a new name that is made unique by appending a number at the end(i.e. _1, _2,...)</summary>
        /// <param name="originalFileName">The original file name to check for.</param>
        /// <param name="directory">The base directory to check for existing files.</param>
        /// <returns>The <paramref name="originalFileName"/> if no file in the passed-in <paramref name="directory"/> exists, otherwise the <paramref name="originalFileName"/> appended with an underscore and a number which will make that name unique.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public static string AppendFileNumber(string originalFileName, System.IO.DirectoryInfo directory) {
            int id = 0;
            string filename = originalFileName;
            string fullPath = System.IO.Path.Combine(directory.FullName, originalFileName);
            if (!System.IO.File.Exists(fullPath))
                return originalFileName;

            do {
                ++id;
                fullPath = System.IO.Path.Combine(directory.FullName, System.IO.Path.GetFileNameWithoutExtension(filename) + "_" + id.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + System.IO.Path.GetExtension(filename));
            }
            while (System.IO.File.Exists(fullPath));

            return System.IO.Path.GetFileNameWithoutExtension(filename) + "_" + id.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + System.IO.Path.GetExtension(filename);
        }

        /// <summary>Converts all invalid file name characters in the passed in <paramref name="originalFileName"/> with underscores.</summary>
        /// <param name="originalFileName">The file name to convert.</param>
        /// <returns>The passed in <paramref name="originalFileName"/> with all invalid file name characters replaced with underscores.</returns>
        public static string EscapeFileName(string originalFileName) {
            System.Text.StringBuilder builder = new StringBuilder(originalFileName.Length);
            foreach (char c in originalFileName) {
                if (!char.IsLetterOrDigit(c) && c != '.' && (Array.BinarySearch<char>(invalidChars, c) >= 0)) {
                    builder.Append('_');
                    continue;
                }
                builder.Append(c);
            }

            System.IO.Path.GetFileName(originalFileName);
            return originalFileName;
        }
    }
}

